
//
// 101122 - AH - Created.
//

package org.soekrat.engine.io;

import java.util.Set;

/**
 * <p>
 *   Converts a
 *   {@code Set<String>} object to a comma separated 
 *   {@code String}. 
 * </p>
 * <p>
 *   Default name on attributes converted like this is "tags".
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
public final class TagsAttributeValueToString extends AttributeValueToString 
{
  /**
   * <p>
   *   Creates for attribute values named "tags".
   * </p>
   */
  public TagsAttributeValueToString(){ super("tags"); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param attrName Name on attributes converted {@code this} way.
   */
  public TagsAttributeValueToString(String attrName){ super(attrName); }

  @SuppressWarnings("unchecked")
  @Override
  public String toString(Object value) 
  {
    Set<String> tags=(Set<String>)value;
    if (tags.isEmpty()) return "";
    StringBuilder sb=new StringBuilder();
    boolean first=true;
    for (String tag : tags)
    {
      if (!first) sb.append(",");
      first=false;
      sb.append(tag);
    }
    return sb.toString();
  }
  
  
}
